package com.petalsgroup.petals.dao.Animation.impl;

import com.petalsgroup.petals.dao.Animation.AnimationDao;
import com.petalsgroup.petals.pojo.*;
import com.petalsgroup.petals.util.JDBCUtils;
import com.petalsgroup.petals.util.RecentlyUtil;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;

public class AnimationDaoImpl implements AnimationDao {
    private final JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public AnimationSimpleDO getAnimationSimple(Integer aid) {
        String sql="select * from animation_simple where aid=?";
        AnimationSimpleDO simpleDO=null;
        try {
            simpleDO=template.queryForObject(sql,new BeanPropertyRowMapper<>(AnimationSimpleDO.class),aid);
        }catch (Exception e){}
        return simpleDO;
    }

    @Override
    public long countAnimationSimple(String search) {
        String sql = "select count(*) from animation_simple where aname like ?";
        long count = template.queryForObject(sql, long.class, search);
        return count;
    }

    @Override
    public List<AnimationSimpleDO> listAnimationSimple(String search, int star, int num) {
        String sql = "select * from animation_simple where aname like ? limit ?,?";
        List<AnimationSimpleDO> list = null;
        list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), search, star, num);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    /**
     * 查询所有动漫简单信息条数
     * @author zqm
     * @return
     */
    @Override
    public long countAnimationSimple() {
        String sql = "select count(*) from animation_simple";
        long count = template.queryForObject(sql, long.class);
        return count;
    }

    /**
     * 查询所有动漫简单信息
     * @param star
     * @param end
     * @author zqm
     * @return
     */
    @Override
    public List<AnimationSimpleDO> listAnimationSimple(int star, int end) {
        String sql = "select * from animation_simple limit ?,?";
        List<AnimationSimpleDO> list = null;
        list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), star, end);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    /**
     * 查询最近动漫简单信息数
     * @author zqm
     * @param time
     * @return
     */
    @Override
    public long countAnimationSimpleRecently(String time) {
        String sql = "select count(*) from animation_simple where create_time > ?";
        long count = template.queryForObject(sql, long.class, time);
        return count;
    }

    /**
     * 查询最近动漫简单信息
     * @author zqm
     * @param star
     * @param num
     * @return
     */
    @Override
    public List<AnimationSimpleDO> listAnimationSimpleRecently(String time, int star, int num) {
        String sql = "select * from animation_simple where create_time > ? order by create_time desc limit ?,?";
        List<AnimationSimpleDO> list = null;
        list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), time, star, num);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    /**
     * 按照更新时间查询
     * @author zqm
     * @param week
     * @param star
     * @param end
     * @return
     */
    @Override
    public List<AnimationSimpleDO> listAnimationSimpleByFlashTime(String week, int star, int end) {
        String sql = "select * from animation_simple where state = '连载中' and flash_time like ? limit ?,?";
        List<AnimationSimpleDO> list = null;
        week = week + "%";
        list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), week, star, end);
        if (list == null || list.size() <= 0 ) {
            return null;
        } else {
            return list;
        }
    }

    @Override
    public List<AnimationSimpleDO> findAnimationByAname(String selectname) {
        selectname = selectname + "%";
        String sql = "select * from animation_simple where aname like ?";
        List<AnimationSimpleDO> list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), selectname);
        return list;
    }

    @Override
    public long countAnimationSimpleBySearch(String sql) {
        long count = template.queryForObject(sql, long.class);
        return count;
    }

    /**
     * 根据需求查询
     * @param sql
     * @param star
     * @param end
     * @return
     */
    @Override
    public List<AnimationSimpleDO> listAnimationSimpleBySearch(String sql, int star, int end) {
        List<AnimationSimpleDO> list = template.query(sql, new BeanPropertyRowMapper<>(AnimationSimpleDO.class), star, end);
        return list;
    }

    @Override
    public AnimationDetailedDO getAnimationDetailed(Integer aid) {
        String sql="select * from animation_detailed where aid=?";
        AnimationDetailedDO detailedDO=null;
        try {
            detailedDO=template.queryForObject(sql,new BeanPropertyRowMapper<>(AnimationDetailedDO.class),aid);
        }catch (Exception e){}
        return detailedDO;
    }

    @Override
    public CategoryDO getCategory(Integer cid) {
        String sql="select * from category where cid=?";
        CategoryDO category=null;
        try {
            category = template.queryForObject(sql, new BeanPropertyRowMapper<>(CategoryDO.class), cid);
        }catch (Exception e){}
        return category;
    }

    @Override
    public List<CommentsDO> getCommentsList(Integer aid) {
        String sql="select * from comments where aid=?";
        List<CommentsDO> list=null;
        try {
            list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class), aid);
        }catch (Exception e){}
        return list;
    }

    @Override
    public List<AnimationSimpleDO> getAnimationMore(Integer cid) {
        String sql="select * from animation_simple where cid=?";
        List<AnimationSimpleDO> list=null;
        try {
            list=template.query(sql,new BeanPropertyRowMapper<>(AnimationSimpleDO.class),cid);
        }catch (Exception e){}
        return list;
    }

    @Override
    public List<AnimationSimpleDO> getAllAnimationSimpleDO() {
        String sql="select * from animation_simple";
        List<AnimationSimpleDO> list=null;
        try {
            list=template.query(sql,new BeanPropertyRowMapper<>(AnimationSimpleDO.class));
        }catch (Exception e){}
        return list;
    }

    @Override
    public List<CategoryDO> getAllCategory() {
        String sql="select c.cid,c.cname from animation_simple a join category c where a.cid=c.cid";
        List<CategoryDO> list=null;
        try {
            list=template.query(sql,new BeanPropertyRowMapper<>(CategoryDO.class));
        }catch (Exception e){}
        return list;
    }

    @Override
    public boolean addCollect(Integer aid,Integer uid) {
        String sql="insert into collect(uid,aid) values(?,?) ";
        int flag = template.update(sql, uid, aid);
        return flag != 0;
    }

    @Override
    public boolean removeCollect(Integer aid,Integer uid) {
        String sql="delete from collect where aid=? and uid=?";
        int flag = template.update(sql, aid, uid);
        return flag != 0;
    }

    @Override
    public boolean findCollect(Integer aid, Integer uid) {
        String sql="select count(*) from collect where aid=? and uid=?";
        Integer query =0;
        try {
            query = template.queryForObject(sql, Integer.class, aid, uid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return query!=0;
    }

    @Override
    public int updateAnimationSimple(AnimationSimpleDO ado) {
        String sql;
        if (ado.getCover_img() != null) {
            sql = "update animation_simple set aname = ?,aname_jp = ?,cover_img = ?,flash_time = ?,state = ?," +
                    "create_time = ?,author = ?,cid = ?, `simple_introduce` = ?, production_company = ? where aid = ?";
            return template.update(sql, ado.getAname(), ado.getAname_jp(), ado.getCover_img(), ado.getFlash_time(),
                    ado.getState(), RecentlyUtil.getStr(ado.getCreate_time()), ado.getAuthor(), ado.getCid(),
                    ado.getSimple_introduce(), ado.getProduction_company(), ado.getAid());
        } else {
            sql = "update animation_simple set aname = ?,aname_jp = ?,flash_time = ?,state = ?," +
                    "create_time = ?,author = ?,cid = ?, `simple_introduce` = ?, production_company = ? where aid = ?";
            return template.update(sql, ado.getAname(), ado.getAname_jp(), ado.getFlash_time(),
                    ado.getState(), RecentlyUtil.getStr(ado.getCreate_time()), ado.getAuthor(), ado.getCid(),
                    ado.getSimple_introduce(), ado.getProduction_company(), ado.getAid());
        }
    }

    @Override
    public int updateAnimationDetail(AnimationDetailedDO ado) {
        String sql;
        if (ado.getPv() != null) {
            sql = "update animation_detailed set pv = ?, detailed = ?, num = ? where aid = ?";
            int update = template.update(sql, ado.getPv(), ado.getDetailed(), ado.getNum(), ado.getAid());
            return update;
        } else {
            sql = "update animation_detailed set detailed = ?, num = ? where aid = ?";
            int update = template.update(sql, ado.getDetailed(), ado.getNum(), ado.getAid());
            return update;
        }
    }

    @Override
    public int insertAnimationSimple(AnimationSimpleDO asd) {
        String sql = "insert into animation_simple (aname, aname_jp, cover_img, flash_time, state, score, create_time, " +
                "author, cid, `simple_introduce`, people_num, production_company) " +
                "value (?,?,?,?,?,?,?,?,?,?,?,?)";
        int update = template.update(sql, asd.getAname(), asd.getAname_jp(), asd.getCover_img(), asd.getFlash_time(),
                asd.getState(), asd.getScore(), RecentlyUtil.getStr(asd.getCreate_time()), asd.getAuthor(), asd.getCid(),
                asd.getSimple_introduce(), asd.getPeople_num(), asd.getProduction_company());
        return update;
    }

    @Override
    public int getAidByAname(AnimationSimpleDO animationSimpleDO) {
        String sql = "select aid from animation_simple where aname = ?";
        Integer aid = template.queryForObject(sql, Integer.class, animationSimpleDO.getAname());
        return aid;
    }

    @Override
    public int insertAnimationDetail(AnimationDetailedDO add) {
        String sql = "insert into animation_detailed (aid, pv, detailed, num) VALUE (?,?,?,?)";
        int update = template.update(sql, add.getAid(), add.getPv(), add.getDetailed(), add.getNum());
        return update;
    }

    @Override
    public int deleteAnimationSimpleByAid(int aid) {
        String sql = "delete from animation_simple where aid = ?";
        return template.update(sql, aid);
    }

    @Override
    public int deleteAnimationDetailByAid(int aid) {
        String sql = "delete from animation_detailed where aid = ?";
        return template.update(sql, aid);
    }

    /**
     * 查询所有出现的年份
     * @author zqm
     * @return
     */
    @Override
    public List<Integer> listAllYear() {
        String sql = "select distinct YEAR(create_time) 'year' from animation_simple order by year desc ";
        List<Integer> list = template.queryForList(sql, Integer.class);
        return list;
    }
}
